package com.stu.serive.impl;

import com.stu.Dto.ClassJobApplicationDTO;
import com.stu.Dto.StuLoginDto;
import com.stu.constant.StatusConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.ClassInformationMapper;
import com.stu.mapper.LoginMapper;
import com.stu.mapper.StuInformationMapper;
import com.stu.pojo.ClassJobApplication;
import com.stu.pojo.Login;
import com.stu.pojo.StuBasicInformation;
import com.stu.serive.ClassInformationService;
import com.stu.serive.LoginService;
import com.stu.vo.ClassJobApplicationVo;
import com.stu.vo.ComplaintVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ClassInformationImpl implements ClassInformationService {

    @Autowired
    private ClassInformationMapper classInformationMapper;
    @Autowired
    private StuInformationMapper stuInformationMapper;

    /**
     * 添加学生申请班委信息
     * @param classJobApplicationDTO
     */
    public void insertClassJobApplication(ClassJobApplicationDTO classJobApplicationDTO) {
        log.info("classJobApplicationDTO:{}",classJobApplicationDTO);
        //获取提交申请的学生的id
        Long currentId = BaseContext.getCurrentId();
        //创建entity对象
        ClassJobApplication classJobApplication = new ClassJobApplication();
        //赋值对象
        BeanUtils.copyProperties(classJobApplicationDTO,classJobApplication);
        //操作对象
        classJobApplication.setStuId(Math.toIntExact(currentId));
        classJobApplication.setApplicationTime(LocalDateTime.now());

        //获取basic数据
        StuBasicInformation stuBasicInformation = stuInformationMapper.selectBasicById(currentId);
        //获取学生的班级外键
        Integer classId = classInformationMapper.selectClassIdByStuClassInfo(stuBasicInformation.getClassInfo());
        //添加班级外键
        classJobApplication.setClassId(classId);
        //操作数据库
        classInformationMapper.insertClassJobApplication(classJobApplication);
        log.info("classJobApplication:{}",classJobApplication);
    }

    /**
     * 查询班委申请进度
     * @return
     */
    public List<ClassJobApplicationVo> selectJobApplication() {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //根据学生id查询 留校申请表和学生基础信息表 获取Vo数据
        List<ClassJobApplicationVo> classJobApplicationVoList = classInformationMapper.selectJobApplication(currentId);

        //对审核状态进行判断
        for (ClassJobApplicationVo vo : classJobApplicationVoList) {
           if(Objects.equals(vo.getStatus(), "1")){
               vo.setStatus(StatusConstant.STATUS_1);
           } else if (Objects.equals(vo.getStatus(), "0")) {
               vo.setStatus(StatusConstant.STATUS_0);
           }else{
               vo.setStatus(StatusConstant.STATUS_2);
           }
        }
        return classJobApplicationVoList;

    }


    /**
     * 查询所有班级信息
     * @return
     */
    public List<ClassJobApplicationVo> classInformationAll() {
        List<ClassJobApplicationVo> classJobApplicationVoList = classInformationMapper.selectJobApplicationAll();
        return classJobApplicationVoList;
    }
}
